project(fontman_library)

set(CFLAGS "-Wall")
set(LIBRARY_OUTPUT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../fontman")

if(NOT KPATHSEA_PATH)
    set(KPATHSEA_PATH "/opt/texlive2007")
endif(NOT KPATHSEA_PATH)

if(UNIX)
    set(EXTRA_FILES dir.c)

    # common options for all Mac OS X platform build
    if(APPLE)
        set(CFLAGS "${CFLAGS} -fno-common")

        # we tune path like this to support libraries installed by macports
        # and fink. 
        set(CMAKE_INCLUDE_PATH 
            "${KPATHSEA_PATH}/include" 
            "/usr/local/include" 
            "/opt/local/include" 
            "/sw/include" 
            "/usr/include")
        set(CMAKE_LIBRARY_PATH "${KPATHSEA_PATH}/lib" 
            "/usr/local/lib" "/opt/local/lib" "/sw/lib" "/usr/lib")
    endif(APPLE)

    # decide which system font listing backend to use, we have: 
    # Mac OS X: fontconfig, ATS
    # Unix    : fontconfig
    # Win32   : fontconfig, win32
    # in Mac OS X and Win32, we prefer OS native backend first, 
    # unless specified in options explictly. 
    if(BACKEND STREQUAL "fontconfig")
        set(USE_FONTCONFIG 1)
    else(BACKEND STREQUAL "fontconfig")
        if(APPLE)
            set(USE_ATS 1)
        # If we are not running Mac OS X, even not specified 
        # explictly, still have to use fontconfig
        else(APPLE)
            set(USE_FONTCONFIG 1)
        endif(APPLE)
    endif(BACKEND STREQUAL "fontconfig")
endif(UNIX)

if(USE_ATS)
    set(EXTRA_FILES ${EXTRA_FILES} ats.c)
    
    find_library(CARBON_LIBRARY Carbon)
    
    set(EXTRA_LIBS ${CARBON_LIBRARY})
    # we don't need extra include path when using framework, 
    # so clean this up
    set(BACKEND_INCLUDE)
    
    add_definitions(-DFONTMAN_BACKEND_ATS)
endif(USE_ATS)

if (USE_FONTCONFIG)
    set(EXTRA_FILES ${EXTRA_FILES} fc.c)
    
    message("CMAKE_LIBRARY_PATH = ${CMAKE_LIBRARY_PATH}")
    find_library(FONTCONFIG_LIBRARY fontconfig)
    find_path(FONTCONFIG_INCLUDE "fontconfig/fontconfig.h")
    message("fontconfig path = ${FONTCONFIG_INCLUDE}")

    set(EXTRA_LIBS ${FONTCONFIG_LIBRARY})
    set(BACKEND_INCLUDE ${FONTCONFIG_INCLUDE})
    
    add_definitions(-DFONTMAN_BACKEND_FC)
endif(USE_FONTCONFIG)

if(NOT FREETYPE_PATH)
    find_path(FREETYPE_INCLUDE ft2build.h)
    find_library(FREETYPE_LIBRARY freetype)
else(NOT FREETYPE_PATH)
    MESSAGE("Looking for FreeType Library and Headers in ${FREETYPE_PATH}")
    find_path(FREETYPE_INCLUDE ft2build.h "${FREETYPE_PATH}/include" NO_DEFAULT_PATH)
    MESSAGE("FREETYPE_INCLUDE = ${FREETYPE_INCLUDE}")
    find_library(FREETYPE_LIBRARY freetype "${FREETYPE_PATH}/lib" NO_DEFAULT_PATH)
    MESSAGE("FREETYPE_LIBRARY = ${FREETYPE_LIBRARY}")
endif(NOT FREETYPE_PATH)

# locate iconv library
find_library(ICONV_LIBRARY iconv)
find_path(ICONV_INCLUDE iconv.h)

find_library(KPATHSEA_LIBRARY kpathsea)
find_path(KPATHSEA_INCLUDE "kpathsea/kpathsea.h")

include_directories(${FREETYPE_INCLUDE} 
                    "${FREETYPE_INCLUDE}/freetype2" 
                    ${KPATHSEA_INCLUDE}
                    ${ICONV_INCLUDE}
                    ${BACKEND_INCLUDE})

add_library(fontman SHARED libfontman.c libfontman.h internal.h ${EXTRA_FILES})
target_link_libraries(fontman ${EXTRA_LIBS} ${KPATHSEA_LIBRARY} 
                        ${FREETYPE_LIBRARY} ${ICONV_LIBRARY})

set_target_properties(fontman PROPERTIES 
                      COMPILE_FLAGS ${CFLAGS})

install(TARGETS fontman 
        DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}/../fontman")
